﻿@using Smartstore.Web.Rendering.Menus
@using Smartstore.Collections
@using Smartstore.Core.Content.Menus

@model MenuModel

@{
    var root = Model.Root;
    if (root == null)
    {
        return;
    }

    // Support both cases: the dropdown-link is Model.Root or the first item if it has children.
    if (root.HasChildren && root.Children.Count == 1 && root.FirstChild.HasChildren)
    {
        root = root.FirstChild;
    }

    var item = root.Value;
}

<div class="cms-menu cms-menu-dropdown" data-menu-name="@(Model.Name?.ToLower())">
    <div class="dropdown">
        <a data-toggle="dropdown" aria-haspopup="true" aria-expanded="false" href="#" rel="nofollow" attrs="@item.GetCombinedAttributes().PrependCssClass("menubar-link")">
            <span sm-language-attributes-for="item">@root.GetItemText(T)</span>
            <i class="fal fa-angle-down menubar-caret"></i>
        </a>
        @{ await CreateDropdown(root.Children); }
    </div>
</div>

@{
    async Task CreateDropdown(IEnumerable<TreeNode<MenuItem>> nodes)
    {
        if (!nodes.Any())
        {
            return;
        }

        var isFirst = true;
        var rtl = WorkContext.WorkingLanguage.Rtl;
        var hasIcons = nodes.Any(x => x.Value.Icon.HasValue());
        var hasImages = nodes.Any(x => x.Value.ImageUrl.HasValue());

        <div class="dropdown-menu @(rtl ? "dropdown-menu-right" : "dropdown-menu-left")">
            @foreach (var node in nodes)
            {
                var item = node.Value;
                if (!item.Visible)
                {
                    continue;
                }

                var itemText = node.GetItemText(T);
                var itemUrl = item.GenerateUrl(this.ViewContext);

                if (item.IsGroupHeader)
                {
                    if (!isFirst)
                    {
                        <div class="dropdown-divider"></div>
                    }
                    if (itemText.HasValue() && item.Text != "[SKIP]")
                    {
                        <div class="dropdown-header menu-header h6">@itemText</div>
                    }
                    isFirst = false;
                    continue;
                }

                var attrs = item.GetCombinedAttributes().PrependCssClass("dropdown-item menu-link");

                <a href="@itemUrl" attrs="attrs">
                    @if (hasIcons)
                    {
                        <i class="fa-fw@(item.Icon.HasValue() ? item.Icon.EnsureStartsWith(" ") : " fas")"></i>
                    }
                    else if (hasImages && item.ImageUrl.HasValue())
                    {
                        <img class="sm-icon" src="@Url.Content(item.ImageUrl)" alt="" />
                    }
                    else if (hasImages)
                    {
                        <i class="fa-fw fas"></i>
                    }
                    <span sm-language-attributes-for="item">@itemText</span>
                </a>

                isFirst = false;
            }
        </div>
    }
}